// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   ChunkHeader.c
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>

#include "../ChunkHeader.h"

/**
 *
 * @param file
 * @return
 */
ChunkHeader* readChunkHeader(FILE* file) {
    ChunkHeader* chunkHeader = malloc(sizeof (ChunkHeader));
    chunkHeader->ck = readCk(file);
    if (strcmp(chunkHeader->ck->ID, "RIFF") != 0) {
        fprintf(stderr, "ckID != 'RIFF'\n");
        exit(EXIT_FAILURE);
    }
    readWAVEID(file, &(chunkHeader->WAVEID));
    if (strcmp(chunkHeader->WAVEID, "WAVE") != 0) {
        fprintf(stderr, "WAVEID != 'WAVE'\n");
        exit(EXIT_FAILURE);
    }
    return chunkHeader;
}

/**
 *
 * @param file
 * @param WAVEID
 */
void readWAVEID(FILE* file, char** WAVEID) {
    *WAVEID = malloc(sizeof (char[5]));
    fgets(*WAVEID, 5, file);
}

/**
 *
 * @param chunkHeader
 */
void freeChunkHeader(ChunkHeader* chunkHeader) {
    freeCk(chunkHeader->ck);
    free(chunkHeader);
}

/**
 *
 * @param chunkHeader
 * @param file
 */
void writeChunkHeader(ChunkHeader* chunkHeader, FILE* file) {
    writeCk(chunkHeader->ck, file);
    writeWAVEID(&chunkHeader->WAVEID, file);
}

/**
 *
 * @param WAVEID
 * @param file
 */
void writeWAVEID(char** WAVEID, FILE* file) {
    fputs(*WAVEID, file);
}
